import com.tj.shopping.entity.Admin;
import com.tj.shopping.entity.Order;
import com.tj.shopping.mapper.AdminMapper;
import com.tj.shopping.mapper.OrderMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Admintest {
    private static SqlSessionFactory factory;

    static {
        String resource = "mybatis.xml";
        InputStream in = null;
        try {
            in = Resources.getResourceAsStream(resource);
            factory = new SqlSessionFactoryBuilder().build(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testAdd() throws Exception {
        SqlSession session = factory.openSession(true);//SQLSession默认事务手动提交 事务自动提交 openSession(true);
        //获取接口代理对象
        AdminMapper mapper = session.getMapper(AdminMapper.class);
        Admin admin = new Admin();
        admin.setAname("123123");
        admin.setAtel("12556778878");
        admin.setAsex("女");
        int result = mapper.add(admin);
        System.out.println(result);
        session.close();

    }


    @Test
    public void testFindbyid() throws Exception {
        SqlSession session = factory.openSession(true);//SQLSession默认事务手动提交 事务自动提交 openSession(true);
        //获取接口代理对象
        AdminMapper mapper = session.getMapper(AdminMapper.class);
        Map<String,Object> m = new HashMap<>();
        m.put("ano",47);
        List<Admin> admins = mapper.findbyid(m);
        System.out.println(admins);
        session.close();

    }

    @Test
    public void testFindall() throws Exception {
        SqlSession session = factory.openSession(true);//SQLSession默认事务手动提交 事务自动提交 openSession(true);
        //获取接口代理对象
        AdminMapper mapper = session.getMapper(AdminMapper.class);
        Map<String,Object> m = new HashMap<>();
        List<Admin> admins = mapper.findall();
        for(Admin admin:admins){
            System.out.println(admin);
        }

        session.close();

    }
    @Test
    public void testUpdate() throws Exception {
        SqlSession session = factory.openSession(true);//SQLSession默认事务手动提交 事务自动提交 openSession(true);
        //获取接口代理对象
        AdminMapper mapper = session.getMapper(AdminMapper.class);
        Admin admin = new Admin();
        admin.setAno(47);
        admin.setAstate(1);
        if (admin.getAstate()==1){
            admin.setAstate(0);
        }else {
            admin.setAstate(1);
        }

        int re=mapper.update(admin);
        System.out.println(re);
        session.close();

    }

    @Test
    public void testaddd() throws Exception {
        SqlSession session = factory.openSession(true);//SQLSession默认事务手动提交 事务自动提交 openSession(true);
        //获取接口代理对象
        OrderMapper mapper = session.getMapper(OrderMapper.class);
        Order order = new Order();
        order.setUno(1);
//        order.setOrdertime("2023-12-14 19:24:29");
        order.setUno(1);
        order.setUno(1);
        order.setUno(1);
        order.setUno(1);
        order.setUno(1);
        order.setUno(1);
        order.setUno(1);

        int re=mapper.add(order);
        System.out.println(re);
        session.close();

    }
}

